package com.example.liaotian.Fragment.DownFragment;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.QuickContactBadge;
import android.widget.TextView;
import android.widget.Toast;

import com.example.liaotian.Bean.User;
import com.example.liaotian.Longin.login.Login;
import com.example.liaotian.R;
import com.example.liaotian.activity.MainActivity;
import com.example.liaotian.activity.MyContentActivity;
import com.example.liaotian.activity.MyMessageActivity;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * 分页我的页
 * A simple {@link Fragment} subclass.
 */
public class MyFragment extends Fragment {


    private Button out,mymessage,mycontent,mycollect;
    private ImageView photo;
    private TextView nikename;


    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_my, container, false);

    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initView();
        getMyInfo();
        initClieck();


    }

    private void initClieck() {
        //退出按钮功能的实现
        out.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setTitle("信息提示")//设置对话框标题

                        .setMessage("是否清除用户数据？")
                        .setPositiveButton("是", new DialogInterface.OnClickListener() {//添加确定按钮

                            @Override
                            public void onClick(DialogInterface dialog, int which) {//确定按钮的响应事件
                                BmobUser.logOut();//清除当前用户数据
                                startActivity(new Intent(getActivity(), Login.class));//跳转到注册页面
                                getActivity().finish();//结束当前activity

                            }
                        }).setNegativeButton("否", new DialogInterface.OnClickListener() {//添加返回按钮

                    @Override
                    public void onClick(DialogInterface dialog, int which) {//响应事件

                    }

                }).show();//在按键响应事件中显示此对话框

            }
        });

        //跳转个人信息界面
        mymessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyMessageActivity.class));
            }
        });
        //跳转我的帖子界面
        mycontent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getActivity(), MyContentActivity.class));
            }
        });
        //跳转我的收藏界面
        mycollect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

            }
        });
    }

    /**
     * 加载个人信息
     */
    private void getMyInfo() {

        BmobUser user = BmobUser.getCurrentUser();
        final String id = user.getObjectId();
        BmobQuery<User> bmobQuery = new BmobQuery<>();

        bmobQuery.getObject(id, new QueryListener<User>() {
            @Override
            public void done(User user, BmobException e) {
                if (e == null){
                    nikename.setText(user.getNickName());
                }else {
                    Toast.makeText(getActivity(),"加载失败",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    private void initView() {
        out = getView().findViewById(R.id.out);
        nikename = getView().findViewById(R.id.nikename);
        mycollect = getView().findViewById(R.id.mycollect);
        mycontent = getView().findViewById(R.id.mycontent);
        mymessage = getView().findViewById(R.id.mymessage);
    }

}
